package com.xiaotu.service.impl;

import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.ObjectUtil;
import com.xiaotu.mapper.LoginMapper;
import com.xiaotu.model.PlayerModel;
import com.xiaotu.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

/**
 * @author zhangxiao
 * @create 2021-11-11 10:13
 */
@Service
@Transactional
public class LoginServiceImpl implements LoginService {

    @Autowired
    private LoginMapper loginMapper;

    @Override
    public PlayerModel checkPassword(String phone, String password) {
        Example example = new Example(PlayerModel.class);
        final Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("phone", phone).andEqualTo("password", password);
        return loginMapper.selectOneByExample(example);
    }

    @Override
    public PlayerModel checkPhone(String phone) {
        Example example = new Example(PlayerModel.class);
        final Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("phone", phone);
        return loginMapper.selectOneByExample(example);
    }

    @Override
    public void validatePlayer(String phone, String playerName) {
        Example example = null;
        example = new Example(PlayerModel.class);
        example.createCriteria().andEqualTo("phone", phone);
        Validator.validateTrue(ObjectUtil.isNull(loginMapper.selectOneByExample(example)), "手机号已存在");
        example = new Example(PlayerModel.class);
        example.createCriteria().andEqualTo("playerName", playerName);
        Validator.validateTrue(ObjectUtil.isNull(loginMapper.selectOneByExample(example)), "玩家名已存在");
    }

}
